// pages/user/help/question/question.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payment_index: 0,
    userInformation: {},
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'userInformation': app.globalData.userInformation,
      'overage': null
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  tixianBtn: function(e){

    var money = e.detail.value;
    console.log(money);
    money = parseInt(money);
    console.log(money);

    if (isNaN(money)) {
      return '';
    }
    if (money > 10000) {
      return 10000;
    }
    if(money < 1) {
      return '';
    }
    return money;
  },
  payment: function(e){
    var _this = this;
    wx.showActionSheet({
      itemList: ['支付宝'],
      success: function (res) {
        var index = res.tapIndex;
        _this.setData({
          payment_index: index
        });
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  tixianAll: function() {
    var overage = app.globalData.incomeStatistics.overage;
    this.setData({
      overage: overage
    });
  },
  formSubmit: function(e){
    var _this = this;
    var amount = e.detail.value.amount;
    var payType = 1;
    
    if (!this.data.userInformation.aliPayId) {
      wx.showModal({
        content: '请绑定支付账号，是否马上绑定？',
        confirmText: '是',
        cancelText: '否',
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../userinformation/userinformation',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }

    if(amount == '') {
      wx.showModal({
        content: '说吧，你想提多少钱？',
        showCancel: false
      })
      return;
    }
    var amount = parseInt(amount);
    
    if(amount < 1) {
      wx.showModal({
        content: '提现金额必须高于1元，否则就是看不起我！',
        showCancel: false
      })
      return;
    }
    if (amount > 10000) {
      wx.showModal({
        content: '提现金额高于10,000元啦，臣妾做不到呀！',
        showCancel: false
      })
      return;
    }
    _this.setData({
      disabled: true
    });
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/withdrawDeposit',
      method: 'POST',
      data: {
        "amount": amount,
        'withdrawDepositType': payType
      },
      header: {
        'content-type': 'application/json',
        'token': app.globalData.token
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 'ok') {

          wx.showToast({
            title: '提现成功',
            icon: 'success',
            duration: 2000
          })

          setTimeout(function () {
            _this.setData({
              disabled: false
            });
            wx.navigateBack({
              delta: 1
            })
          }, 2000);

        } else {
          _this.setData({
            disabled: false
          });
          app.responseCheck(_this, res.data, '/pages/user/withdraw/withdraw');
        }
      },
      fail: function (e) {
        _this.setData({
          disabled: false
        });
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  }
})